package pv.finder;

import java.util.ArrayList;
import java.util.List;

import pv.finder.MySetting.MAPMODE;
import pv.finder.MySetting.ROUTEMODE;
import pv.finder.MySetting.SEARCHMODE;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

/**
 * Created by hitle on 6/12/13.
 */
public class FMOption extends Fragment implements
		ExpandableListView.OnChildClickListener,
		ExpandableListView.OnGroupClickListener {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.fm_menu, null);

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		ExpandableListView elv = ((ExpandableListView) getView().findViewById(
				R.id.elvSetting));

		MyListAdapter adap = initData();
		elv.setAdapter(adap);
		elv.setOnGroupClickListener(this);
		elv.setOnChildClickListener(this);
		for (int i = 0; i < adap.getGroupCount(); i++) {
			elv.expandGroup(i);
		}

	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub

		Fragment f = getActivity().getSupportFragmentManager()
				.findFragmentById(R.id.main_content);
		if (f instanceof FMMap) {
			// map type normal

			MySetting settings = MySetting.getInstance();
			if (groupPosition == 1) {

				/* ward */if (childPosition == 0) {
					settings.setSearch(SEARCHMODE.WARD);

				}/* district */else if (childPosition == 1) {
					settings.setSearch(SEARCHMODE.DISTRIC);

				}/* nearlest */else if (childPosition == 2) {
					settings.setSearch(SEARCHMODE.NEARLESS);
				}

			}
			settings.saveSetting(getActivity());

			MyListAdapter mla = ((MyListAdapter) parent
					.getExpandableListAdapter());

			((ItemMenu) mla.getGroup(groupPosition))
					.setChoosedId(((ChildItemMenu) mla.getChild(groupPosition,
							childPosition)).getId());

			((MyListAdapter) parent.getExpandableListAdapter())
					.notifyDataSetChanged();

			// ((FMMap) f).removeSubContent();
		}
		return false;
	}

	protected MyListAdapter initData() {
		// TODO Auto-generated method stub
		MyListAdapter adapter = new MyListAdapter(getActivity());
		List<ItemMenu> listData = new ArrayList<ItemMenu>();
		String[] settins = getResources().getStringArray(R.array.option);
		// sync_data
		ItemMenu sync_data = new ItemMenu(0, settins[0], R.drawable.icon_sync);
		listData.add(sync_data);
		// search_mode
		ItemMenu searchmode = new ItemMenu(1, settins[1], R.drawable.icon_search);
		searchmode.setListChild(new ChildItemMenu[] {
		/* normap */
		new ChildItemMenu(2, settins[2]),
		/* Hybrid */
		new ChildItemMenu(3, settins[3]),
		/* Satellite */
		new ChildItemMenu(4, settins[4]) });
		MySetting mySetting = MySetting.getInstance();
		for (int i = 0; i < searchmode.getListChild().length; i++) {

			if (i == mySetting.getSearch().ordinal()) {
				searchmode.setChoosedId(searchmode.getListChild()[i].getId());
			}

		}
		listData.add(searchmode);

		// Route Travel Modes
		ItemMenu about = new ItemMenu(5, settins[5], R.drawable.icon_about);
		// full screen
		listData.add(about);

		adapter.setListData(listData);

		return adapter;

	}

	class MyListAdapter extends BaseExpandableListAdapter {

		Context context;
		List<ItemMenu> listData;

		public MyListAdapter(Context context) {
			this.context = context;
			listData = new ArrayList<ItemMenu>();
		}

		public MyListAdapter(Context context, List<ItemMenu> listData) {
			this.context = context;
			this.listData = listData;
		}

		public List<ItemMenu> getListData() {
			return listData;
		}

		public void setListData(List<ItemMenu> listData) {
			this.listData = listData;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return listData.get(groupPosition).getListChild()[childPosition];
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ChildItemMenu itemChild = (ChildItemMenu) getChild(groupPosition,
					childPosition);
			ItemMenu parentItem = (ItemMenu) getGroup(groupPosition);

			if (convertView == null) {
				LayoutInflater infalInflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				convertView = infalInflater.inflate(R.layout.item_menu_child,
						null);

			}

			((TextView) convertView.findViewById(R.id.txtName))
					.setText(itemChild.getName());

			// boolean isCheck = (groupPosition == 0 /* map type */&& MySetting
			// .getInstance().getMap().ordinal() == childPosition)
			// || (groupPosition == 1 /* route type */&& MySetting
			// .getInstance().getRoute().ordinal() == childPosition);
			if (itemChild.getId() == parentItem.getChoosedId()) {

				((ImageView) convertView.findViewById(R.id.icon_check))
						.setVisibility(View.VISIBLE);

			} else {
				((ImageView) convertView.findViewById(R.id.icon_check))
						.setVisibility(View.INVISIBLE);
			}

			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generat ed method stub
			return listData.get(groupPosition).getListChild() != null ? listData
					.get(groupPosition).getListChild().length : 0;
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return listData.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return listData.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ItemMenu headerInfo = (ItemMenu) getGroup(groupPosition);

			if (convertView == null) {
				LayoutInflater inf = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inf.inflate(R.layout.item_menu, null);
				convertView.setBackgroundResource(R.drawable.bg_sub_menu);
			}

			((TextView) convertView.findViewById(R.id.txtName))
					.setText(headerInfo.getName());

			((TextView) convertView.findViewById(R.id.txtName))
					.setCompoundDrawablesWithIntrinsicBounds(
							headerInfo.getDrawableRes(), 0, 0, 0);

			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}

	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		// TODO Auto-generated method stub
		return true;
	}

}
